﻿using System;
using System.Collections.Generic;

namespace UniRx.Operators
{
	// Token: 0x020002F3 RID: 755
	internal class SelectManyObservable<TSource, TCollection, TResult> : OperatorObservableBase<TResult>
	{
		// Token: 0x06000FFB RID: 4091 RVA: 0x00049EA0 File Offset: 0x000482A0
		public SelectManyObservable(IObservable<TSource> source, Func<TSource, IObservable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.collectionSelector = collectionSelector;
			this.resultSelector = resultSelector;
		}

		// Token: 0x06000FFC RID: 4092 RVA: 0x00049EC3 File Offset: 0x000482C3
		public SelectManyObservable(IObservable<TSource> source, Func<TSource, int, IObservable<TCollection>> collectionSelector, Func<TSource, int, TCollection, int, TResult> resultSelector) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.collectionSelectorWithIndex = collectionSelector;
			this.resultSelectorWithIndex = resultSelector;
		}

		// Token: 0x06000FFD RID: 4093 RVA: 0x00049EE6 File Offset: 0x000482E6
		public SelectManyObservable(IObservable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.collectionSelectorEnumerable = collectionSelector;
			this.resultSelector = resultSelector;
		}

		// Token: 0x06000FFE RID: 4094 RVA: 0x00049F09 File Offset: 0x00048309
		public SelectManyObservable(IObservable<TSource> source, Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, int, TCollection, int, TResult> resultSelector) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.collectionSelectorEnumerableWithIndex = collectionSelector;
			this.resultSelectorWithIndex = resultSelector;
		}

		// Token: 0x06000FFF RID: 4095 RVA: 0x00049F2C File Offset: 0x0004832C
		protected override IDisposable SubscribeCore(IObserver<TResult> observer, IDisposable cancel)
		{
			if (this.collectionSelector != null)
			{
				return new SelectManyObservable<TSource, TCollection, TResult>.SelectManyOuterObserver(this, observer, cancel).Run();
			}
			if (this.collectionSelectorWithIndex != null)
			{
				return new SelectManyObservable<TSource, TCollection, TResult>.SelectManyObserverWithIndex(this, observer, cancel).Run();
			}
			if (this.collectionSelectorEnumerable != null)
			{
				return new SelectManyObservable<TSource, TCollection, TResult>.SelectManyEnumerableObserver(this, observer, cancel).Run();
			}
			if (this.collectionSelectorEnumerableWithIndex != null)
			{
				return new SelectManyObservable<TSource, TCollection, TResult>.SelectManyEnumerableObserverWithIndex(this, observer, cancel).Run();
			}
			throw new InvalidOperationException();
		}

		// Token: 0x04000955 RID: 2389
		private readonly IObservable<TSource> source;

		// Token: 0x04000956 RID: 2390
		private readonly Func<TSource, IObservable<TCollection>> collectionSelector;

		// Token: 0x04000957 RID: 2391
		private readonly Func<TSource, int, IObservable<TCollection>> collectionSelectorWithIndex;

		// Token: 0x04000958 RID: 2392
		private readonly Func<TSource, IEnumerable<TCollection>> collectionSelectorEnumerable;

		// Token: 0x04000959 RID: 2393
		private readonly Func<TSource, int, IEnumerable<TCollection>> collectionSelectorEnumerableWithIndex;

		// Token: 0x0400095A RID: 2394
		private readonly Func<TSource, TCollection, TResult> resultSelector;

		// Token: 0x0400095B RID: 2395
		private readonly Func<TSource, int, TCollection, int, TResult> resultSelectorWithIndex;

		// Token: 0x020002F4 RID: 756
		private class SelectManyOuterObserver : OperatorObserverBase<TSource, TResult>
		{
			// Token: 0x06001000 RID: 4096 RVA: 0x00049FA2 File Offset: 0x000483A2
			public SelectManyOuterObserver(SelectManyObservable<TSource, TCollection, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001001 RID: 4097 RVA: 0x00049FC0 File Offset: 0x000483C0
			public IDisposable Run()
			{
				this.collectionDisposable = new CompositeDisposable();
				this.sourceDisposable = new SingleAssignmentDisposable();
				this.collectionDisposable.Add(this.sourceDisposable);
				this.sourceDisposable.Disposable = this.parent.source.Subscribe(this);
				return this.collectionDisposable;
			}

			// Token: 0x06001002 RID: 4098 RVA: 0x0004A018 File Offset: 0x00048418
			public override void OnNext(TSource value)
			{
				IObservable<TCollection> observable;
				try
				{
					observable = this.parent.collectionSelector(value);
				}
				catch (Exception error)
				{
					this.OnError(error);
					return;
				}
				SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
				this.collectionDisposable.Add(singleAssignmentDisposable);
				SelectManyObservable<TSource, TCollection, TResult>.SelectManyOuterObserver.SelectMany observer = new SelectManyObservable<TSource, TCollection, TResult>.SelectManyOuterObserver.SelectMany(this, value, singleAssignmentDisposable);
				singleAssignmentDisposable.Disposable = observable.Subscribe(observer);
			}

			// Token: 0x06001003 RID: 4099 RVA: 0x0004A084 File Offset: 0x00048484
			public override void OnError(Exception error)
			{
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x06001004 RID: 4100 RVA: 0x0004A0E0 File Offset: 0x000484E0
			public override void OnCompleted()
			{
				this.isStopped = true;
				if (this.collectionDisposable.Count == 1)
				{
					object obj = this.gate;
					lock (obj)
					{
						try
						{
							this.observer.OnCompleted();
						}
						finally
						{
							base.Dispose();
						}
					}
				}
				else
				{
					this.sourceDisposable.Dispose();
				}
			}

			// Token: 0x0400095C RID: 2396
			private readonly SelectManyObservable<TSource, TCollection, TResult> parent;

			// Token: 0x0400095D RID: 2397
			private CompositeDisposable collectionDisposable;

			// Token: 0x0400095E RID: 2398
			private object gate = new object();

			// Token: 0x0400095F RID: 2399
			private bool isStopped;

			// Token: 0x04000960 RID: 2400
			private SingleAssignmentDisposable sourceDisposable;

			// Token: 0x020002F5 RID: 757
			private class SelectMany : OperatorObserverBase<TCollection, TResult>
			{
				// Token: 0x06001005 RID: 4101 RVA: 0x0004A164 File Offset: 0x00048564
				public SelectMany(SelectManyObservable<TSource, TCollection, TResult>.SelectManyOuterObserver parent, TSource value, IDisposable cancel) : base(parent.observer, cancel)
				{
					this.parent = parent;
					this.sourceValue = value;
					this.cancel = cancel;
				}

				// Token: 0x06001006 RID: 4102 RVA: 0x0004A18C File Offset: 0x0004858C
				public override void OnNext(TCollection value)
				{
					TResult value2;
					try
					{
						value2 = this.parent.parent.resultSelector(this.sourceValue, value);
					}
					catch (Exception error)
					{
						this.OnError(error);
						return;
					}
					object gate = this.parent.gate;
					lock (gate)
					{
						this.observer.OnNext(value2);
					}
				}

				// Token: 0x06001007 RID: 4103 RVA: 0x0004A210 File Offset: 0x00048610
				public override void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						try
						{
							this.observer.OnError(error);
						}
						finally
						{
							base.Dispose();
						}
					}
				}

				// Token: 0x06001008 RID: 4104 RVA: 0x0004A274 File Offset: 0x00048674
				public override void OnCompleted()
				{
					this.parent.collectionDisposable.Remove(this.cancel);
					if (this.parent.isStopped && this.parent.collectionDisposable.Count == 1)
					{
						object gate = this.parent.gate;
						lock (gate)
						{
							try
							{
								this.observer.OnCompleted();
							}
							finally
							{
								base.Dispose();
							}
						}
					}
				}

				// Token: 0x04000961 RID: 2401
				private readonly SelectManyObservable<TSource, TCollection, TResult>.SelectManyOuterObserver parent;

				// Token: 0x04000962 RID: 2402
				private readonly TSource sourceValue;

				// Token: 0x04000963 RID: 2403
				private readonly IDisposable cancel;
			}
		}

		// Token: 0x020002F6 RID: 758
		private class SelectManyObserverWithIndex : OperatorObserverBase<TSource, TResult>
		{
			// Token: 0x06001009 RID: 4105 RVA: 0x0004A314 File Offset: 0x00048714
			public SelectManyObserverWithIndex(SelectManyObservable<TSource, TCollection, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x0600100A RID: 4106 RVA: 0x0004A330 File Offset: 0x00048730
			public IDisposable Run()
			{
				this.collectionDisposable = new CompositeDisposable();
				this.sourceDisposable = new SingleAssignmentDisposable();
				this.collectionDisposable.Add(this.sourceDisposable);
				this.sourceDisposable.Disposable = this.parent.source.Subscribe(this);
				return this.collectionDisposable;
			}

			// Token: 0x0600100B RID: 4107 RVA: 0x0004A388 File Offset: 0x00048788
			public override void OnNext(TSource value)
			{
				int arg = this.index++;
				IObservable<TCollection> observable;
				try
				{
					observable = this.parent.collectionSelectorWithIndex(value, arg);
				}
				catch (Exception error)
				{
					this.OnError(error);
					return;
				}
				SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
				this.collectionDisposable.Add(singleAssignmentDisposable);
				SelectManyObservable<TSource, TCollection, TResult>.SelectManyObserverWithIndex.SelectManyObserver observer = new SelectManyObservable<TSource, TCollection, TResult>.SelectManyObserverWithIndex.SelectManyObserver(this, value, arg, singleAssignmentDisposable);
				singleAssignmentDisposable.Disposable = observable.Subscribe(observer);
			}

			// Token: 0x0600100C RID: 4108 RVA: 0x0004A40C File Offset: 0x0004880C
			public override void OnError(Exception error)
			{
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x0600100D RID: 4109 RVA: 0x0004A468 File Offset: 0x00048868
			public override void OnCompleted()
			{
				this.isStopped = true;
				if (this.collectionDisposable.Count == 1)
				{
					object obj = this.gate;
					lock (obj)
					{
						try
						{
							this.observer.OnCompleted();
						}
						finally
						{
							base.Dispose();
						}
					}
				}
				else
				{
					this.sourceDisposable.Dispose();
				}
			}

			// Token: 0x04000964 RID: 2404
			private readonly SelectManyObservable<TSource, TCollection, TResult> parent;

			// Token: 0x04000965 RID: 2405
			private CompositeDisposable collectionDisposable;

			// Token: 0x04000966 RID: 2406
			private object gate = new object();

			// Token: 0x04000967 RID: 2407
			private bool isStopped;

			// Token: 0x04000968 RID: 2408
			private SingleAssignmentDisposable sourceDisposable;

			// Token: 0x04000969 RID: 2409
			private int index;

			// Token: 0x020002F7 RID: 759
			private class SelectManyObserver : OperatorObserverBase<TCollection, TResult>
			{
				// Token: 0x0600100E RID: 4110 RVA: 0x0004A4EC File Offset: 0x000488EC
				public SelectManyObserver(SelectManyObservable<TSource, TCollection, TResult>.SelectManyObserverWithIndex parent, TSource value, int index, IDisposable cancel) : base(parent.observer, cancel)
				{
					this.parent = parent;
					this.sourceValue = value;
					this.sourceIndex = index;
					this.cancel = cancel;
				}

				// Token: 0x0600100F RID: 4111 RVA: 0x0004A51C File Offset: 0x0004891C
				public override void OnNext(TCollection value)
				{
					TResult value2;
					try
					{
						value2 = this.parent.parent.resultSelectorWithIndex(this.sourceValue, this.sourceIndex, value, this.index++);
					}
					catch (Exception error)
					{
						try
						{
							this.observer.OnError(error);
						}
						finally
						{
							base.Dispose();
						}
						return;
					}
					object gate = this.parent.gate;
					lock (gate)
					{
						this.observer.OnNext(value2);
					}
				}

				// Token: 0x06001010 RID: 4112 RVA: 0x0004A5D8 File Offset: 0x000489D8
				public override void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						try
						{
							this.observer.OnError(error);
						}
						finally
						{
							base.Dispose();
						}
					}
				}

				// Token: 0x06001011 RID: 4113 RVA: 0x0004A63C File Offset: 0x00048A3C
				public override void OnCompleted()
				{
					this.parent.collectionDisposable.Remove(this.cancel);
					if (this.parent.isStopped && this.parent.collectionDisposable.Count == 1)
					{
						object gate = this.parent.gate;
						lock (gate)
						{
							try
							{
								this.observer.OnCompleted();
							}
							finally
							{
								base.Dispose();
							}
						}
					}
				}

				// Token: 0x0400096A RID: 2410
				private readonly SelectManyObservable<TSource, TCollection, TResult>.SelectManyObserverWithIndex parent;

				// Token: 0x0400096B RID: 2411
				private readonly TSource sourceValue;

				// Token: 0x0400096C RID: 2412
				private readonly int sourceIndex;

				// Token: 0x0400096D RID: 2413
				private readonly IDisposable cancel;

				// Token: 0x0400096E RID: 2414
				private int index;
			}
		}

		// Token: 0x020002F8 RID: 760
		private class SelectManyEnumerableObserver : OperatorObserverBase<TSource, TResult>
		{
			// Token: 0x06001012 RID: 4114 RVA: 0x0004A6DC File Offset: 0x00048ADC
			public SelectManyEnumerableObserver(SelectManyObservable<TSource, TCollection, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001013 RID: 4115 RVA: 0x0004A6ED File Offset: 0x00048AED
			public IDisposable Run()
			{
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x06001014 RID: 4116 RVA: 0x0004A700 File Offset: 0x00048B00
			public override void OnNext(TSource value)
			{
				IEnumerable<TCollection> enumerable;
				try
				{
					enumerable = this.parent.collectionSelectorEnumerable(value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				using (IEnumerator<TCollection> enumerator = enumerable.GetEnumerator())
				{
					bool flag = true;
					while (flag)
					{
						flag = false;
						TResult value2 = default(TResult);
						try
						{
							flag = enumerator.MoveNext();
							if (flag)
							{
								value2 = this.parent.resultSelector(value, enumerator.Current);
							}
						}
						catch (Exception error2)
						{
							try
							{
								this.observer.OnError(error2);
							}
							finally
							{
								base.Dispose();
							}
							break;
						}
						if (flag)
						{
							this.observer.OnNext(value2);
						}
					}
				}
			}

			// Token: 0x06001015 RID: 4117 RVA: 0x0004A81C File Offset: 0x00048C1C
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06001016 RID: 4118 RVA: 0x0004A854 File Offset: 0x00048C54
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0400096F RID: 2415
			private readonly SelectManyObservable<TSource, TCollection, TResult> parent;
		}

		// Token: 0x020002F9 RID: 761
		private class SelectManyEnumerableObserverWithIndex : OperatorObserverBase<TSource, TResult>
		{
			// Token: 0x06001017 RID: 4119 RVA: 0x0004A88C File Offset: 0x00048C8C
			public SelectManyEnumerableObserverWithIndex(SelectManyObservable<TSource, TCollection, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001018 RID: 4120 RVA: 0x0004A89D File Offset: 0x00048C9D
			public IDisposable Run()
			{
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x06001019 RID: 4121 RVA: 0x0004A8B0 File Offset: 0x00048CB0
			public override void OnNext(TSource value)
			{
				int arg = this.index++;
				IEnumerable<TCollection> enumerable;
				try
				{
					enumerable = this.parent.collectionSelectorEnumerableWithIndex(value, arg);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				using (IEnumerator<TCollection> enumerator = enumerable.GetEnumerator())
				{
					int num = 0;
					bool flag = true;
					while (flag)
					{
						flag = false;
						TResult value2 = default(TResult);
						try
						{
							flag = enumerator.MoveNext();
							if (flag)
							{
								value2 = this.parent.resultSelectorWithIndex(value, arg, enumerator.Current, num++);
							}
						}
						catch (Exception error2)
						{
							try
							{
								this.observer.OnError(error2);
							}
							finally
							{
								base.Dispose();
							}
							break;
						}
						if (flag)
						{
							this.observer.OnNext(value2);
						}
					}
				}
			}

			// Token: 0x0600101A RID: 4122 RVA: 0x0004A9F4 File Offset: 0x00048DF4
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0600101B RID: 4123 RVA: 0x0004AA2C File Offset: 0x00048E2C
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000970 RID: 2416
			private readonly SelectManyObservable<TSource, TCollection, TResult> parent;

			// Token: 0x04000971 RID: 2417
			private int index;
		}
	}
}
